﻿Imports ZedGraph
Imports System.ComponentModel

Public Class FindJobOfferControlKrypton

    Private mypaint As Boolean

    Private iUserID As Integer

    Dim bSalaryValidated As Boolean
    Public Property UserID() As Integer
        Get
            Return iUserID
        End Get
        Set(ByVal value As Integer)
            iUserID = value
        End Set
    End Property

    Private Sub FindJobOffersControl_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        mypaint = True
        Me.JobTypeTableAdapter.Fill(Me.DatabaseDataSet.JobType)
        Me.CountryTableAdapter.Fill(Me.DatabaseDataSet.Country)
        Me.CityTableAdapter.Fill(Me.DatabaseDataSet.City)

    End Sub

    '===============================================================
    ' Charts - A pie chart with the weights of the offer
    '---------------------------------------------------------------
    'Purpose : Paints a pie chart which containing the weights of the 
    '          selected offer in the datagridView. Users a external library
    '          for charts (ZedGraph).
    '
    ' Author : John Paraskevopoulos, 13 December 2012, 6 January 2013
    '===============================================================
    Protected Sub paintGraph(ByVal rowNumber As Integer)
        Me.ZedGraphControl1.GraphPane.CurveList.Clear()
        Me.ZedGraphControl1.GraphPane.GraphObjList.Clear()
        Me.ZedGraphControl1.Invalidate()
        Dim mypane As GraphPane = Me.ZedGraphControl1.GraphPane

        If (mypaint) Then
            ' Set the GraphPane title
            mypane.Title.Text = "Job offer weights" & Chr(15)
            mypane.Title.FontSpec.IsItalic = True
            mypane.Title.FontSpec.Size = 24.0F
            mypane.Title.FontSpec.Family = "Times New Roman"

            ' Fill the pane background with a color gradient
            mypane.Fill = New Fill(Color.White, Color.Goldenrod, 45.0F)
            ' No fill for the chart background
            mypane.Chart.Fill.Type = FillType.None

            ' Set the legend to an arbitrary location
            mypane.Legend.Position = LegendPos.Float
            mypane.Legend.Location = New Location(0.99F, 0.01F, CoordType.PaneFraction, _
                        AlignH.Right, AlignV.Top)
            mypane.Legend.FontSpec.Size = 17.0F
            mypane.Legend.IsHStack = False

            Dim selectedRowCells As DataGridViewCellCollection = Me.DataGridViewJobOffers.Rows(rowNumber).Cells
            Dim vv As Double() = {selectedRowCells.Item(2).Value, selectedRowCells.Item(4).Value, selectedRowCells.Item(6).Value, selectedRowCells.Item(8).Value, selectedRowCells.Item(10).Value, selectedRowCells.Item(12).Value}
            Dim ss As String() = {"Degree", "Master", "Phd", "Foreign Language", "Army", "Job Type"}

            mypane.AddPieSlices(vv, ss)
            Me.ZedGraphControl1.AxisChange()
        Else
            ' Set the GraphPane title
            mypane.Title.Text = "Lucene - Free text searching" & Chr(15)
            mypane.Title.FontSpec.IsItalic = True
            mypane.Title.FontSpec.Size = 40.0F
            mypane.Title.FontSpec.Family = "Times New Roman"
            ' Fill the pane background with a color gradient
            mypane.Fill = New Fill(Color.White, Color.Goldenrod, 45.0F)
            ' No fill for the chart background
            mypane.Chart.Fill.Type = FillType.None
            Me.ZedGraphControl1.AxisChange()
        End If
    End Sub

    Private Sub DataGridViewJobOffers_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridViewJobOffers.CellClick
        paintGraph(e.RowIndex)
        FillEmployerData(Me.DataGridViewJobOffers.Rows(e.RowIndex).Cells.Item("JobOfferEmployerUserID").Value)
    End Sub

    Private Sub DataGridViewJobOffers_DataBindingComplete(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewBindingCompleteEventArgs) Handles DataGridViewJobOffers.DataBindingComplete
        paintGraph(0)
        FillEmployerData(Me.DataGridViewJobOffers.Rows(0).Cells.Item("JobOfferEmployerUserID").Value)
    End Sub

    Public Sub setPaint()
        mypaint = True
    End Sub

    Public Sub unsetPaint()
        mypaint = False
    End Sub

    '===============================================================
    ' Filling the controls with employer's data
    '---------------------------------------------------------------
    'Purpose : Fills the controls with employer's company title,    
    '          description and email, who's id is the parameter of
    '          the function.
    '          
    ' Author : Chatzigeorgiou Konstantinos, 5 January 2013
    '===============================================================
    Private Sub FillEmployerData(ByVal id As Integer)
        Dim userTable As New DatabaseDataSet.UserDataTable
        Me.UserTableAdapter.FillByID(userTable, id)

        Dim employerTable As New DatabaseDataSet.EmployerDataTable
        Me.EmployerTableAdapter.FillByID(employerTable, id)

        Me.txtCompanyTitle.Text = employerTable.Rows(0).Item("EmployerCompanyTitle")
        Me.txtCompanyDescription.Text = employerTable.Rows(0).Item("EmployerDescription")
        Me.txtCompanyEmail.Text = userTable.Rows(0).Item("UserEmail")
    End Sub

    Dim sStatus As String

    Private Sub btnSendEmployerEmail_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSendEmployerEmail.Click

        ' Checking if there are no results
        If (DataGridViewJobOffers.Rows.Count = 0) Then
            MessageBox.Show("As much i enjoy sending emails to employers all over the world, a job offer is not selected!")
            Return
        End If

        Dim bgWorker As New BackgroundWorker
        AddHandler bgWorker.DoWork, AddressOf EmailBackgroundWorker_DoWork
        AddHandler bgWorker.RunWorkerCompleted, AddressOf EmailBackgroundWorker_RunWorkerCompleted
        bgWorker.RunWorkerAsync()

    End Sub

    Private Sub EmailBackgroundWorker_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs)
        Dim userTable As New DatabaseDataSet.UserDataTable
        Me.UserTableAdapter.FillByID(userTable, UserID)

        Dim mailManager As MailManager = New MailManager(My.Settings.MailServerSettings)
        Dim time As DateTime = DateTime.Now

        sStatus = mailManager.SendEmail(Me.txtCompanyEmail.Text, "Employee interested for the job", _
                                            "Date: " + time.ToString + vbCrLf + vbCrLf + _
                                            "First Name: " + userTable.Rows(0).Item("UserFirstName") + vbCrLf + _
                                            "Last Name: " + userTable.Rows(0).Item("UserLastName") + vbCrLf + _
                                            "Email: " + userTable.Rows(0).Item("UserEmail"))
    End Sub

    Private Sub EmailBackgroundWorker_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs)
        MessageBox.Show(sStatus)
    End Sub

    Private Sub txtMinSalary_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtMinSalary.KeyPress
        If Not IsNumeric(e.KeyChar) AndAlso e.KeyChar <> ControlChars.Back Then
            e.Handled = True
        End If
    End Sub

    Private Sub txtMinSalary_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtMinSalary.Validating
        If txtMinSalary.TextLength = 0 Then
            bSalaryValidated = False
            MinSalaryErrorProvider.SetError(txtMinSalary, "Salary can't be empty")
        ElseIf Convert.ToDouble(txtMinSalary.Text) < 300 Then
            bSalaryValidated = False
            MinSalaryErrorProvider.SetError(txtMinSalary, "Salary can't be less than 300€")
        Else
            bSalaryValidated = True
            MinSalaryErrorProvider.Clear()
        End If
    End Sub

End Class
